import base64
import json
import requests
import hashlib
import warnings
from ddddocr import DdddOcr


def encrypt_by_des(t):
    salt = "TuD00Iqz4ge7gzIe2rmjSAFFKtaIBmnr8S"
    if not isinstance(t, str):
        warnings.warn("Parameter is not a string. Attempting to convert it.")
        t = str(t)
    combined = t + salt
    md5_hash = hashlib.md5(combined.encode('utf-8')).hexdigest()
    return md5_hash


def get_img_code():
    url = 'https://www.hfax.com/pc-api/common/imageCode/login'
    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "https://www.hfax.com/login.html",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "apiVersion": "1.9",
        "ds": "home",
        "sec-ch-ua-mobile": "?0"
    }
    # 发送GET请求
    response = requests.get(url, headers=headers)
    return response.json()


def recognize_captcha(base64str):
    # 移除Base64前缀（如果有的话）
    if base64str.startswith('data:image/png;base64,'):
        base64str = base64str[len('data:image/png;base64,'):]

    # 解码Base64字符串为图片数据
    image_data = base64.b64decode(base64str)

    # 使用ddddocr识别验证码
    ocr = DdddOcr()
    result = ocr.classification(image_data)

    return result


def login(username, password, img_code, img_token):
    import requests

    url = 'https://www.hfax.com/pc-api/user/login'

    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Type": "application/json;charset=UTF-8",
        "Origin": "https://www.hfax.com",
        "Pragma": "no-cache",
        "Referer": "https://www.hfax.com/login.html",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "apiVersion": "1.9",
        "distinctid": "19077df67a7c0c-02ffc3b3cd120a-15313374-1327104-19077df67a8164c",
        "ds": "home",
        "sec-ch-ua-mobile": "?0"
    }

    data = {
        "username": F"{username}",
        "password": F"{password}",
        "imgCode": F"{img_code}",
        "imgToken": F"{img_token}"
    }
    data = json.dumps(data, separators=(',', ':'))

    # 发送GET请求
    response = requests.post(url, headers=headers, data=data)
    return response.json()


if __name__ == '__main__':
    img_data = get_img_code()
    img_token = img_data['data']['token']
    igm_base64 = img_data['data']['base64Str']
    print(img_token)
    print(igm_base64)
    captcha = recognize_captcha(igm_base64)
    print("识别结果:", captcha)
    login_res = login('1003206180', '1003206180', captcha, img_token)
    print(login_res)
